package com.mi.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mi.entity.AuthInfo;
import com.mi.entity.ResponseMsg;
import com.mi.entity.RoleInfo;
import com.mi.service.AuthService;
import com.mi.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class RoleController {

    @Autowired
    RoleService roleService;

    @Autowired
    AuthService authService;

    //根据条件查询角色列表
    @RequestMapping("roleList")
    public String adminRole(HttpServletRequest req, RoleInfo roleInfo){
        System.out.println(roleInfo);
        PageHelper.startPage(roleInfo.getPageNum(),roleInfo.getPageSize());
        List<RoleInfo> rolePojos = roleService.queryRoles(roleInfo);
        System.out.println(rolePojos);
        PageInfo<RoleInfo> list = new PageInfo<>(rolePojos);
        req.setAttribute("rname",roleInfo.getRname());
        req.setAttribute("pageInfo",list);
        return "roleList";
    }

    //修改角色状态
    @RequestMapping("updRoleStatus")
    @ResponseBody
    public ResponseMsg updRoleStatus(RoleInfo roleInfo){
        ResponseMsg responseMsg = new ResponseMsg();
        boolean b = roleService.updRoleStatus(roleInfo);
        if(b){
            responseMsg.setResult("0000");
        }else{
            responseMsg.setResult("0001");
            responseMsg.setResultMsg("修改失败");
        }
        return responseMsg;
    }

    //根据id删除角色
    @RequestMapping("delRole")
    @ResponseBody
    public ResponseMsg delRole(String id){
        boolean b = roleService.delOneRole(id);
        ResponseMsg responseMsg = new ResponseMsg();
        if(b){
            responseMsg.setResult("0000");
        }else{
            responseMsg.setResult("0001");
            responseMsg.setResultMsg("删除失败");
        }
        return responseMsg;
    }

    //根据一组id删除角色
    @RequestMapping("delRoles")
    @ResponseBody
    public ResponseMsg delRoles(String ids){
        ResponseMsg responseMsg = new ResponseMsg();
        if(ids==null||"".equals(ids)){
            responseMsg.setResult("0001");
            responseMsg.setResultMsg("删除失败");
        }else{
        boolean b = roleService.delRoles(ids);
            if(b){
                responseMsg.setResult("0000");
            }else{
                responseMsg.setResult("0001");
                responseMsg.setResultMsg("删除失败");
            }
        }
        return responseMsg;
    }

    //转发到添加角色界面
    @RequestMapping("roleAdd")
    public String roleAdd(){
        return "roleAdd";
    }

    //转发到更改角色界面
    @RequestMapping("roleEdit")
    public String roleEdit(HttpServletRequest req,int rid){
        try {
            RoleInfo rolePojos = roleService.queryRoleAuth(String.valueOf(rid));
            ObjectMapper objectMapper = new ObjectMapper();
            List<AuthInfo> auths = rolePojos.getAuths();
            List<AuthInfo> authPojos = authService.queryAuth(new AuthInfo());
            String str = "";
            for (AuthInfo auth1 : authPojos) {
                for(AuthInfo auth2 : auths){
                    if(auth1.getAuid() == auth2.getAuid()){
                        str+=auth1.getAuid()+",";
                        auth1.setCheck(true);
                        break;
                    }
                }
            }
            if(str!=""){
                str = str.substring(0, str.length() - 1);
            }
            String s = objectMapper.writeValueAsString(authPojos);
            req.setAttribute("auths_edit",s);
            req.setAttribute("role_auth",str);
            req.setAttribute("role",rolePojos);
        }catch (Exception e){
            e.printStackTrace();
            return "errer";
        }
        return "roleEdit";
    }

    //添加角色
    @RequestMapping("addRole")
    @ResponseBody
    public ResponseMsg addRole(RoleInfo roleInfo){
        ResponseMsg responseMsg = new ResponseMsg();
        System.out.println(roleInfo);
        boolean b = false;
        try {
            b = roleService.addRole(roleInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(b){
                responseMsg.setResult("0000");
            }else{
                responseMsg.setResult("0001");
                responseMsg.setResultMsg("添加失败");
            }
            return responseMsg;
        }
    }

    //更新角色
    @RequestMapping("updRole")
    @ResponseBody
    public ResponseMsg updRole(RoleInfo roleInfo,String[] auths_edit,String[] role_auth){
        System.out.println(roleInfo);
        ResponseMsg responseMsg = new ResponseMsg();
        boolean b = false;
        try {
            b = roleService.updRole(roleInfo,auths_edit,role_auth);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(b){
                responseMsg.setResult("0000");
            }else{
                responseMsg.setResult("0001");
                responseMsg.setResultMsg("修改失败");
            }
            return responseMsg;
        }
    }
}
